import random
bank = {12345678:{"username":"陈伟","password":"123456","country":"china","prince":"江苏","street":"昌平","door":"007","money":888,"bank_name":"工商银行"}}
bank_name = "工商银行"
welcome = '''
*******************************************
*             中国工商银行                  *
*             账户管理系统                  *
*                V1.0                     *
*******************************************

*1. 开户                                   *
*2. 存钱                                   *
*3. 取钱                                   *
*4. 转账                                   *
*5. 查询                                   *
*6. Bye！                                  *
*******************************************
'''
def judgepwd(password) :
    if len(password) == 6 :
        return int(password)
    else :
        pwd = input("您输入的密码错误，请重新输入:")
        return judgepwd(pwd)

def bank_adduesr() :
    account = random.randint(10000000,99999999)
    username = input("请输入您的姓名:")
    password = input("请输入您的六位数密码:")
    password = judgepwd(password)
    country = input("请输入您的国家:")
    prince = input("请输入您的省份:")
    street = input("请输入您的所在街道:")
    door = input("请输入您的门牌号:")
    money = 0
    jd = user_add(account,username,password,country,prince,street,door,money)
    if jd == 3 :
        print("数据库已满")
    elif jd == 2 :
        print("用户已存在")
    elif jd == 1 :
        print("添加用户成功")
        info = '''
        --------工商银行--------
        账号:%s
        姓名:%s
        密码:%s
        地址： 
            国家:%s
            省份:%s
            街道:%s
            门牌号:%s
        账户余额:%s
        开户行:%s
        '''
        print(info%(account,username,password,country,prince,street,door,money,bank_name))
#添加用户的业务逻辑
def user_add(account,username,password,country,prince,street,door,money) :
    if len(bank) >= 100 :
        return 3
    elif account in bank :
        return 2
    else :
        bank[account] = {
            "username" : username,
            "password" : password,
            "country" : country,
            "prince" : prince,
            "street" : street,
            "door" : door,
            "money" : money,
            "bank_name" : bank_name
        }
        return 1
#判断账号和存钱金额输入格式是否有误
def access_judge(a):
    while True:
        if a.isdigit() :
            a = int(a)
            return a
            break
        else:
            b = input("您输入有误，请重新输入:")
            return access_judge(b)

#存钱的业务逻辑
def access1(account,money) :
    if account in bank :
        bank[account]["money"] = bank[account]["money"] + money
        return True
    else :
        return False
#存钱的输入和输出的操作
def access() :
    account = access_judge(input("请输入您的账号："))
    money = access_judge(input("请输入您要存款的金额："))
    p = access1(account,money)
    if p == True :
        print("用户", bank[account]["username"], "您当前的余额为:", bank[account]["money"])
    elif p == False :
        print("账户不存在")


'''
def1   输入和输出的操作
def2   执行里边的业务逻辑
'''

#判断账号、密码和取钱金额输入格式是否有误
def take_judge(c):
    while True:
        if c.isdigit() :
            c = int(c)
            return c
            break
        else:
            d = input("您输入格式有误，请重新输入:")
            return access_judge(d)
#取钱的输入和输入的操作
def take() :
    account = take_judge(input("请输入您的账号:"))
    password = take_judge(input("请输入您的密码:"))
    money = take_judge(input("请输入您需要取款的金额:"))
    q = take1(account,password,money)
    if q == 1 :
        print("账户不存在")
    elif q == 2 :
        print("密码输入错误")
    elif q == 3 :
        print("账户余额不足")
    elif q == 0 :
        print("您的账户余额为:",bank[account]["money"])

#取钱的业务逻辑
def take1(account,password,money) :
    if account not in bank :
        return 1
    elif password != int(bank[account]["password"]) :
        return 2
    elif bank[account]["money"] < money :
        return 3
    else :
        bank[account]["money"] = bank[account]["money"] - money
        return 0

#判断输入的格式是否正确
def s_m_judge(e) :
    while True:
        if e.isdigit() :
            e = int(e)
            return e
            break
        else:
            f = input("您输入格式有误，请重新输入:")
            return access_judge(f)

#转账的输入和输出的操作
def send_money() :
    account_chu = s_m_judge(input("请输入转出的账号:"))
    account_ru = s_m_judge(input("请输入转入的账号:"))
    password = s_m_judge(input("请输入转出账号的密码:"))
    money = s_m_judge(input("请输入转账的金额:"))
    m = send_money1(account_chu,account_ru,password,money)
    if m == 1 :
        print(bank)
        print("账号不存在")
    elif m == 2 :
        print("密码不正确")
    elif m == 3 :
        print("账户余额不足")
    elif m == 0 :
        print("转出的账户余额为:",bank[account_chu]["money"])
        print("转入的账户余额为:",bank[account_ru]["money"])

#转账的业务逻辑
def send_money1(account_chu,account_ru,password,money) :
    if account_chu not in bank or account_ru not in bank :
        return 1
    elif password != int(bank[account_chu]["password"]) :
        return 2
    elif bank[account_chu]["money"] < money :
        return 3
    else :
        bank[account_chu]["money"] = bank[account_chu]["money"] - money
        bank[account_ru]["money"] = bank[account_ru]["money"] + money
        return 0

#判断输入格式是否正确
def inquire_judge(g) :
    while True:
        if g.isdigit() :
            g = int(g)
            return g
            break
        else:
            h = input("您输入格式有误，请重新输入:")
            return access_judge(h)

#查询账户功能的输入和输出的操作
def inquire() :
    account = inquire_judge(input("请输入您要查询的账号:"))
    password = inquire_judge(input("请输入您要查询的账号密码:"))
    n = inquire1(account,password)


#查询账户功能的业务逻辑
def inquire1(account,password) :
    if account not in bank :
        print("该用户不存在")
    elif password != int(bank[account]["password"]) :
        print("密码不正确")
    else :
        info = '''
        账号:%s
        密码:%s
        余额:%s元
        地址:
           国家:%s
           省份:%s
           街道:%s
           门牌号:%s
        开户行:%s
        '''
        password = bank[account]["password"]
        money = bank[account]["money"]
        country = bank[account]["country"]
        prince = bank[account]["prince"]
        street = bank[account]["street"]
        door = bank[account]["door"]
        bank_name = bank[account]["bank_name"]
        print(info%(account ,password,money,country,prince,street,door,bank_name))
#退出
def quit() :
    quit = print("Bye!")
    return quit

while True :
    print(welcome)
    n = int(input("请输入您需要使用的业务编号:"))
    if n == 1 :
        bank_adduesr()
    elif n == 2 :
        access()
    elif n == 3 :
        take()
    elif n == 4 :
        send_money()
    elif n == 5 :
        inquire()
    elif n == 6 :
        quit()
        break
    else :
        print("不存在该业务，请您重新选择")


